package net.kesumu.general.android.opengl.object;

import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.*;
import static net.kesumu.general.android.opengl.util.Constants.BYTES_PER_FLOAT;

import java.util.List;

import net.kesumu.general.android.opengl.object.Geometry.Line;
import net.kesumu.general.android.opengl.object.Geometry.Point;
import net.kesumu.general.android.opengl.object.ObjectBuilder.DrawCommand;
import net.kesumu.general.android.opengl.object.ObjectBuilder.GeneratedData;
import net.kesumu.general.android.opengl.program.ColorShaderProgram;
import net.kesumu.general.android.opengl.util.VertexArray;

public class YushabuMessageLine {
    
    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int COLOR_COMPONENT_COUNT = 3;
    private static final int STRIDE = 
            (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * BYTES_PER_FLOAT;
    
    private final VertexArray vertexArray;
    private final List<DrawCommand> drawList;

    public YushabuMessageLine(Point startPoint, Point endPoint, int color, int numPoints) {
        Line line = new Line(startPoint, endPoint);
        
        GeneratedData generatedData = ObjectBuilder.createYushabuMessageLine(
                        line, MESSAGE_LINE, color, numPoints);

        vertexArray = new VertexArray(generatedData.vertexData);
        drawList = generatedData.drawList;
    }
    
    public void bindData(ColorShaderProgram colorProgram) {
        vertexArray.setVertexAttribPointer(0,
            colorProgram.getPositionAttributeLocation(),
            POSITION_COMPONENT_COUNT, STRIDE);
        vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT,
                colorProgram.getColorAttributeLocation(),
                COLOR_COMPONENT_COUNT, STRIDE);
    }
    
    public void draw() {
        for (DrawCommand drawCommand : drawList) {
            drawCommand.draw();
        }
    }

}
